#Include <..\include\Common_For_PageManager.au3>
#Include <GuiButton.au3>
#Include <GuiTab.au3>

AutoItSetOption("MustDeclareVars", 1)   ;Variables must be pre-declared.

Dim Const $iNumber 							= Common_GetNumberInExcelSheet($oExcel,$sFormDispatchingFunctionName)
Global $sCusMsg,$sFailLogMsg
Dim Const $iCurrentSheetTotalLineNumber 	= $iNumber[0]   ;Get all number of rows in current excel sheet.
Dim Const $sFunctionFolderPath 				= "C:\Form Dispatching\"  
Dim Const $CorrectResutPath 				= $sFunctionFolderPath&"Correct Result\"   
Dim Const $PerformResultPath 				= $sFunctionFolderPath&"Perform Result\"   ;Proformed result file path.

Dim Const $sSampleFolderPath 				= $sFunctionFolderPath&"Sample\"  
Dim Const $CommonWindowCtrlInfro 			= '[CLASS:#32770]'
Dim Const $sTemplateSample 					= $sSampleFolderPath & 'Suit.JPG'
Dim Const $sSupportFormatFolderPath 		= $sSampleFolderPath&'Support Format\'
Global Const $sExcelSheetName = 'Form Dispatching'
Global Const $iRescolumn = 14
;Key Value:
Dim Const $sSaveAsTypeCtrlInfo 				= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'SaveAsType' ))
Dim Const $sEditMenuHotKey 					= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'EditMenuHotKey')
Dim Const $sToolsMenuHotKey 				= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'ToolsMenu')
Dim Const $sViewMenuHotKey 					= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'ViewMenuHotKey')
Dim Const $sFileMenuHotKey 					= ReadIniKeyValue($sPCodeIniPath, $sSectionNameThumbanil, 'FileMenuHotKey')
Dim Const $sMyComputerName 					= ReadIniKeyValue($sPCodeIniPath, $sSectionNameTreeView, 'MyComputerName')
Dim Const $sPasswordDialogTitle 			= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFAX, 'PasswordDialogTitle')

Dim Const $sFormWizardDialogTitle 			= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'FormWizardDialogTitle')
Dim Const $sAddNewFormButton 				= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'AddNewFormButton' ))
Dim Const $sEditSelectedFormButton 			= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'EditSelectedFormButton' ))
Dim Const $sRemoveSelectedFormButton		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'RemoveSelectedFormButton' ))
Dim Const $sFormsListBox 					= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'FormsListBox' ))
Dim Const $sCloseButton 					= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'CloseButton' ))
Dim Const $sBrowseForFolderTreeView	 		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'BrowseForFolderTreeView' ))
Dim Const $sBrowseForFolderDialogTitle 		= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'BrowseForFolderDialogTitle')
Dim Const $sEnterFormNameDialogEditBox 		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'EnterFormNameDialogEditBox' ))
Dim Const $sEnterFormNameDialogTitle 		= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'EnterFormNameDialogTitle')
Dim Const $sSelectTemplatesforFormTitle 	= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'SelectTemplatesforFormTitle')
Dim Const $sAddTemplateButton 				= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'AddTemplateButton' ))
Dim Const $sDeleteTemplateButton 			= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'DeleteTemplateButton' ))
Dim Const $sChangeFolderButton 				= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'ChangeFolderButton' ))
Dim Const $sBackButton 						= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'BackButton' ))
Dim Const $sNextButton 						= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'NextButton' ))
Dim Const $sFinishButton 					= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'FinishButton' ))
Dim Const $sCancelButton 					= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'CancelButton' ))
Dim Const $sFormDispatchingResults 			= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'FormDispatchingResults')
Dim Const $sExportDispatchingResults 		= Number(ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'ExportDispatchingResults' ))
Dim Const $sExportSaveAsDialogTitle 		= ReadIniKeyValue($sPCodeIniPath, $sSectionNameFormDispatching, 'ExportSaveAsDialogTitle')


Local $file = FileOpen($sSampleFolderPath & "C:\DoubleByte.txt", 0)
Local $DoubleByte = FileReadLine($file,14)



Func SetDispatchResultsPath($path)
	ControlClick($sFormDispatchingResults, '' ,$sExportDispatchingResults)   ;Exports results button.
	Common_SleepTime(3)
	ControlSetText($sExportSaveAsDialogTitle, '' ,1152, $path)
	Common_SleepTime(1)
	ControlClick($sExportSaveAsDialogTitle , '' , 'Button2')   ;Save button
	Common_SleepTime(3)
EndFunc

Func AutoDispath()
	OpenMenu("Edit")
	Send('d')
	Sleep(300)
	Send('d')
	Sleep(300)
	send('{right}')
	Sleep(300)
	send('d')
	Sleep(1000)
EndFunc


Func ChangePath($path)
	ControlClick("",'', $sChangeFolderButton)   ;Change the folder button.
	Common_SleepTime(3)		
	Local $hTree = ControlGetHandle($sBrowseForFolderDialogTitle, "", "SysTreeView321")
	Local $hItemFound1 = _GUICtrlTreeView_FindItem($hTree, @UserName)
	_GUICtrlTreeView_Expand($hTree,$hItemFound1,False)
	Sleep(300)
	Local $hItemFound2 = _GUICtrlTreeView_FindItem($hTree, $sMyComputerName)
	_GUICtrlTreeView_Expand($hTree,$hItemFound2)	
	Sleep(500)	
	Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, $sDiskC_DiskVolumeName)
	_GUICtrlTreeView_Expand($hTree,$hItemFound)	
	Sleep(300)		
	Switch $path
	Case "SysDir"
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, 'Windows')
		_GUICtrlTreeView_Expand($hTree,$hItemFound)	
		Sleep(300)	
		_GUICtrlTreeView_ClickItem($hTree, $hItemFound)	
		Sleep(300)	
	Case "LongPath"
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, $sSectionNameFormDispatching)
		_GUICtrlTreeView_Expand($hTree,$hItemFound)	
		Sleep(300)	
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, '0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111')
		_GUICtrlTreeView_Expand($hTree,$hItemFound)	
		Sleep(300)			
		_GUICtrlTreeView_ClickItem($hTree, $hItemFound)	
		Sleep(300)	
	Case "DoubleBytePath"
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, $sSectionNameFormDispatching)
		_GUICtrlTreeView_Expand($hTree,$hItemFound)	
		Sleep(300)	
		Local $hItemFound = _GUICtrlTreeView_FindItem($hTree, $DoubleByte)
		_GUICtrlTreeView_Expand($hTree,$hItemFound)	
		Sleep(300)			
		_GUICtrlTreeView_ClickItem($hTree, $hItemFound)	
		Sleep(300)
	EndSwitch
	ControlClick('','',1)
	Common_SleepTime(2)
EndFunc

Func AddTemplate($sPath)
	ControlClick($CommonWindowCtrlInfro, 'Template:',$sAddTemplateButton)   ;CLick add tempalte button.
	Common_SleepTime(2)
	ControlSetText('Open','',1152,$sPath)
	Sleep(500)
	ControlClick('Open','','Button2')   ;OK button.
	Common_SleepTime(6)
EndFunc

Func AddFormName($sName)
	OpenFormDispatchManagerDlg()
	ControlClick($sFormWizardDialogTitle,"",$sAddNewFormButton)   ;Add new form button.
	Common_SleepTime(2)
	ControlSetText($sEnterFormNameDialogTitle,'',$sEnterFormNameDialogEditBox,$sName)
	Sleep(500)
	ControlClick($sEnterFormNameDialogTitle,'',1)   ;Add ok button.
	Common_SleepTime(2)
EndFunc

Func OpenFormDispatchManagerDlg()
	OpenMenu("Edit")
	Send('d')
	Sleep(300)
	Send('d')
	Sleep(300)
	send('{right}')
	Sleep(300)
	send('{m}')
	Sleep(1000)
EndFunc

Func DeleteLastTimeSavedFile($sName)
	#cs
	If FileExists($sName) Then
		FileDelete($sName)
	EndIf
	#ce
EndFunc

Func SetStampAnnotation()
	;Add Anonation
	MouseClick('left', 1255,285)
	Send('{Down}')
	Sleep(300)
	Send('{ENTER}')
	MouseClick('left', 426,261)
	Common_SleepTime(1)	
EndFunc

Func OpenMenu($sMenu)
	Switch $sMenu
	Case "Edit"
		Send("!" & $sEditMenuHotKey)	
		Common_SleepTime(2)	
	Case "Tools"
		Send("!" & $sToolsMenuHotKey)	
		Common_SleepTime(2)	
	Case "View"
		Send("!" & $sViewMenuHotKey)	
		Common_SleepTime(2)	
	Case "File"
		Send("!" & $sFileMenuHotKey)	
		Common_SleepTime(2)
	EndSwitch	
EndFunc

Func DeletePerformResult()
	Local $FileList = _FileListToArray ($PerformResultPath,"*", 0)
	If Not @error Then
		If $FileList[0] >= 1 Then
			For $i = 1 to $FileList[0]
				FileDelete ($PerformResultPath & $FileList[$i])
			Next
			Return 1
		EndIf
	EndIf
	Return @error
EndFunc

Func InitializeStatusForFormDispatching()
	Local $FileList = _FileListToArray ($sFunctionFolderPath,"*", 1)
	If Not @error Then
		If $FileList[0] >= 1 Then
			For $i = 1 to $FileList[0]
				FileDelete ($sFunctionFolderPath & $FileList[$i])
			Next
			Return 1
		EndIf
	EndIf
	Return @error
EndFunc